// pages/home/home.js
import { get, post } from '../../utils/api';

// const floorFilter = function (floor) {

// };

const otherFilter = [() => true, i => i.status === 0, i => i.status !== 0, i => i.id === "A102"];

const randNumber = (end) => Math.floor((Math.random() * (end)));
const randDate = () => {
    // let date = +new Date() - randNumber(0, 10000000);
    const date = `2020-07-16 ${randNumber(24)}:${randNumber(24)}:${randNumber(24)}`;
    // console.log(date);
    return date;
};
const makeData = tablesList => {
    const list = tablesList.map(i => {
        if (i.status !== 0) {
            // console.log('...');
            i.maxUser = randNumber(10) || 10;
            i.users = randNumber(i.maxUser) || 1;
            i.dishes = randNumber(10) || 5;
            i.number = randNumber(i.dishes) || 1;
            // console.log(randDate());
            i.startAt = +new Date(randDate())
            console.log('1', i.startAt);
        } else {
            i.maxUser = 10;
            i.users = 0;
        }
        return i;
    });
    console.log(list);
    return [...list];
};

Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 楼层导航
        topNavActive: 0,
        // 底部按钮
        tabBarActive: 0,
        // 当前选中过滤按钮
        filterBtnIndex: 0,
        // 桌台的状态样式类名
        tableStatus: ['empty', 'using', 'all-send'],
        // 选中桌子id
        selectedTableId: '',
        // 桌台数据
        tablesList: [
            { floor: 1, id: 'A100', maxUser: 10, user: 0, status: 0, number: 2,
                dishes: [
                    {id: 'A10001', status: true},
                    {id: 'A10002', status: true},
                    {id: 'A10003', status: false},
                    {id: 'A10004', status: false},
                    {id: 'A10005', status: false},
            ]},
            { floor: 2, id: 'B101', maxUser: 10, user: 5, status: 1, number: 3,
                startAt: + new Date('2020-07-16 00:00:00'),
                dishes: [
                    {id: 'B10001', status: true, price: 25, count: 1},
                    {id: 'B10002', status: true, price: 100, count: 1},
                    {id: 'B10003', status: true, price: 120, count: 1},
                    {id: 'B10004', status: false, price: 30, count: 1},
                    {id: 'B10005', status: false, price: 12, count: 1},
            ]},
            { floor: 3, id: 'C101', maxUser: 10, user: 5, status: 2, number: 5,
                startAt: + new Date('2020-07-16 00:00:00'),
                dishes: [
                    {id: 'B10001', status: true, price: 25, count: 1},
                    {id: 'B10002', status: true, price: 100, count: 2},
                    {id: 'B10003', status: true, price: 120, count: 1},
                    {id: 'B10004', status: true, price: 30, count: 1},
                    {id: 'B10005', status: true, price: 12, count: 1},
            ]}
            ,
            { floor: 3, id: 'C101', maxUser: 10, user: 5, status: 2, number: 5,
                startAt: + new Date('2020-07-16 00:00:00'),
                dishes: [
                    {id: 'B10001', status: true, price: 25, count: 1},
                    {id: 'B10002', status: true, price: 100, count: 2},
                    {id: 'B10003', status: true, price: 120, count: 1},
                    {id: 'B10004', status: true, price: 30, count: 1},
                    {id: 'B10005', status: true, price: 12, count: 1},
            ]}
            ,
            { floor: 3, id: 'C101', maxUser: 10, user: 5, status: 2, number: 5,
                startAt: + new Date('2020-07-16 00:00:00'),
                dishes: [
                    {id: 'B10001', status: true, price: 25, count: 1},
                    {id: 'B10002', status: true, price: 100, count: 2},
                    {id: 'B10003', status: true, price: 120, count: 1},
                    {id: 'B10004', status: true, price: 30, count: 1},
                    {id: 'B10005', status: true, price: 12, count: 1},
            ]}
            ,
            { floor: 3, id: 'C101', maxUser: 10, user: 5, status: 2, number: 5,
                startAt: + new Date('2020-07-16 00:00:00'),
                dishes: [
                    {id: 'B10001', status: true, price: 25, count: 1},
                    {id: 'B10002', status: true, price: 100, count: 2},
                    {id: 'B10003', status: true, price: 120, count: 1},
                    {id: 'B10004', status: true, price: 30, count: 1},
                    {id: 'B10005', status: true, price: 12, count: 1},
            ]}
            ,
            { floor: 3, id: 'C101', maxUser: 10, user: 5, status: 2, number: 5,
                startAt: + new Date('2020-07-16 00:00:00'),
                dishes: [
                    {id: 'B10001', status: true, price: 25, count: 1},
                    {id: 'B10002', status: true, price: 100, count: 2},
                    {id: 'B10003', status: true, price: 120, count: 1},
                    {id: 'B10004', status: true, price: 30, count: 1},
                    {id: 'B10005', status: true, price: 12, count: 1},
            ]},
            { floor: 3, id: 'C101', maxUser: 10, user: 5, status: 2, number: 5,
                startAt: + new Date('2020-07-16 00:00:00'),
                dishes: [
                    {id: 'B10001', status: true, price: 25, count: 1},
                    {id: 'B10002', status: true, price: 100, count: 2},
                    {id: 'B10003', status: true, price: 120, count: 1},
                    {id: 'B10004', status: true, price: 30, count: 1},
                    {id: 'B10005', status: true, price: 12, count: 1},
            ]},
            { floor: 3, id: 'C101', maxUser: 10, user: 5, status: 2, number: 5,
                startAt: + new Date('2020-07-16 00:00:00'),
                dishes: [
                    {id: 'B10001', status: true, price: 25, count: 1},
                    {id: 'B10002', status: true, price: 100, count: 2},
                    {id: 'B10003', status: true, price: 120, count: 1},
                    {id: 'B10004', status: true, price: 30, count: 1},
                    {id: 'B10005', status: true, price: 12, count: 1},
            ]},
            { floor: 3, id: 'C101', maxUser: 10, user: 5, status: 2, number: 5,
                startAt: + new Date('2020-07-16 00:00:00'),
                dishes: [
                    {id: 'B10001', status: true, price: 25, count: 1},
                    {id: 'B10002', status: true, price: 100, count: 2},
                    {id: 'B10003', status: true, price: 120, count: 1},
                    {id: 'B10004', status: true, price: 30, count: 1},
                    {id: 'B10005', status: true, price: 12, count: 1},
            ]},
            { floor: 3, id: 'C101', maxUser: 10, user: 5, status: 2, number: 5,
                startAt: + new Date('2020-07-16 00:00:00'),
                dishes: [
                    {id: 'B10001', status: true, price: 25, count: 1},
                    {id: 'B10002', status: true, price: 100, count: 2},
                    {id: 'B10003', status: true, price: 120, count: 1},
                    {id: 'B10004', status: true, price: 30, count: 1},
                    {id: 'B10005', status: true, price: 12, count: 1},
            ]}
        ],
        // 过滤数据
        filterList: []
    },

    // 底部导航切换事件
    tabBarOnChange({ detail }) {
        // console.log('tabbar', detail);
        this.setData({
            tabBarActive: detail
        });
    },

    // 过滤按钮点击事件
    onFilter({ target: { dataset: { idx }}}) {
        this.setData({
            filterBtnIndex: idx
        });
        // console.log(this.data.cbs);
        const { topNavActive, filterBtnIndex } = this.data;
        // console.log('topNavActive', topNavActive, 'filterBtnIndex', filterBtnIndex);
        this.filterTableList(i => !topNavActive || i.floor === topNavActive, otherFilter[filterBtnIndex]);
    },

    // 扫码
    onScan() {
        wx.scanCode({
          success(res) {
              console.log('res', res);
          }
        })
    },

    // 点击桌子事件
    onTableClick({ currentTarget: { dataset: { id }}}) {
        console.log('id', id);
        this.setData({
            selectedTableId: id
        });
        const { tablesList } = this.data;
        const table = tablesList.find(i => i.id === id);
        (table.status === 0) && wx.navigateTo({
          url: '/pages/order/order',
        })
    },
    async getPartSeatInfo() {
        const res = wx.getLaunchOptionsSync();
        // console.log('launch res', res);

        const { data: { data: { seatsInfo }}} = await post('http://192.168.35.54:82/seat/getPartSeatInfo', {});
        // wx.request({
        //   url: 'http://192.168.35.54:82/seat/getPartSeatInfo',
        //   method: 'POST',
        //   data: {},
        //   success: res => {
        //       console.log('res', res);
        //   }
        // });
        // return;
        // console.log(res2);
        this.setData({
            tablesList: seatsInfo
        });

        const { tablesList } = this.data;
        this.setData({
            tablesList: makeData(tablesList)
        });

        const { topNavActive, filterBtnIndex } = this.data;
        // console.log('topNavActive', topNavActive, 'filterBtnIndex', filterBtnIndex);
        this.filterTableList(i => !topNavActive || i.floor === topNavActive, otherFilter[filterBtnIndex]);
    },
    onFloorFilter({ detail: { index }}) {
        // console.log(e);
        // const { topNavActive } = this.data;
        // console.log(topNavActive);
        this.setData({
            topNavActive: index
        });
        const { topNavActive, filterBtnIndex } = this.data;
        // console.log('topNavActive', topNavActive, 'filterBtnIndex', filterBtnIndex);
        this.filterTableList(i => !topNavActive || i.floor === topNavActive, otherFilter[filterBtnIndex]);
    },
    // 过滤数据
    filterTableList(filterFloor = () => true, filterOther = () => true) {
        const { tablesList } = this.data;
        const floorList = tablesList.filter(filterFloor) || [];
        // console.log('floorList', floorList);
        const otherList = floorList.filter(filterOther) || [];
        // console.log('otherList', otherList);

        this.setData({
            filterList: otherList
        });
    },
    // goPlace
    goPlace() {
        wx.switchTab({
          url: '/pages/index/index',
        })
    },
    // goPay
    goPay() {},
    // goVip
    goVip() {},
    // goMy
    goMy() {},
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getPartSeatInfo();
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})